const db = require('../db/db')
const {paging} = require('../utils/paging')
const {formatDate,endMonth,nextEndMonth,startMonth} = require('../utils/date')
module.exports = {
    // 添加会员
    ADDVIP: async (ctx, next) => {
        const {id,vipName,sex,vipPhone,openTime,expires,born,payMethod,openMoney,vipRank,money,score,openStaff,belongToShop} =ctx.request.body 
        const oldCreateTime = new Date()
        const createTime = formatDate(oldCreateTime)
        try {
            let res =  await db.promise()
            .query('insert into viplist(id,vipName,sex,vipPhone,openTime,expires,born,payMethod,openMoney,vipRank,money,score,openStaff,createTime,belongToShop) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
            [id,vipName,sex,vipPhone,openTime,expires,born,payMethod,openMoney,vipRank,money,score,openStaff,createTime,belongToShop])
            if (res[0].affectedRows > 0) {
                ctx.body = {
                    code: 200,
                    msg: '添加会员成功'
                }
            }
        } catch (error) {
            console.log(error);
            ctx.body = {
                code: 500,
                msg: '添加会员失败'+error
            }
        }
       
    },
    // 获取vip
    GETVIP:async (ctx, next)=>{
        const {startPage,endPage} = await paging(ctx)
        const {belongToShop} = ctx.request.query
        let [total] = await db.promise().query('select * from viplist ')
        try {
            let [res] = await db.promise().query('select id,vipName,sex,vipPhone,openTime,expires,vipRank,consumed,money,score,openStaff,status from viplist where belongToShop=? order by createTime desc limit ?,?',
            [belongToShop,startPage,endPage])
            ctx.body = {
                code:201,
                msg:'获取vip信息成功',
                total:total.length,
                viplist:res
            }
        } catch (error) {
            ctx.body = {
                code:201,
                msg:'获取vip信息失败'+error
            }
        }
    },
    // 搜索会员
    SEARCHVIP:async (ctx, next)=>{
        const {keyWord, belongToShop} = ctx.request.body
        try {
            let [idRes] = await db.promise().query('select id,vipName,sex,vipPhone,openTime,expires,vipRank,consumed,money,score,openStaff,status from viplist where id=? and belongToShop=?' ,[keyWord, belongToShop])
            let [vipName] = await db.promise().query('select id,vipName,sex,vipPhone,openTime,expires,vipRank,consumed,money,score,openStaff,status from viplist where vipName=? and belongToShop=?' ,[keyWord, belongToShop])
            let [vipPhone] = await db.promise().query('select id,vipName,sex,vipPhone,openTime,expires,vipRank,consumed,money,score,openStaff,status from viplist where vipPhone=? and belongToShop=?' ,[keyWord, belongToShop])
            if(idRes.length !==0){
                ctx.body = {
                    code:200,
                    msg:"搜索成功",
                    viplist:idRes
                }
            }else if(vipName.length !==0){
                ctx.body = {
                    code:200,
                    msg:"搜索成功",
                    viplist:vipName
                }
            }else if(vipPhone.length !==0){
                ctx.body = {
                    code:200,
                    msg:"搜索成功",
                    viplist:vipPhone
                }
            }else{
                ctx.body = {
                    code:201,
                    msg:"没有该会员",
                }
            }
        } catch (error) {
            ctx.body = {
                code:500,
                msg:"搜索失败" + error
            }
        }
    },
    // 禁用(启用)会员
    // DELETEVIP:async (ctx, next)=>{
    //     const {id,status } = ctx.request.body
    //     if(status==0){
    //         // 禁用
    //         try {
    //             let [res] =await db.promise().query('update viplist set status=1 where id=?',id)
    //             if(res.affectedRows !==0){
    //                 ctx.body = {
    //                     code:200,
    //                     msg:"禁用成功"
    //                 }
    //             }else{
    //                 ctx.body = {
    //                     code:200,
    //                     msg:"找不到该会员"
    //                 }
    //             }
    //         } catch (error) {
    //             ctx.body = {
    //                 code:500,
    //                 msg:"禁用失败"+error
    //             }
    //         }
    //     }else{
    //         // 启用
    //         try {
    //             let [res] =await db.promise().query('update viplist set status=0 where id=?',id)
    //             if(res.affectedRows !==0){
    //                 ctx.body = {
    //                     code:200,
    //                     msg:"启用成功"
    //                 }
    //             }else{
    //                 ctx.body = {
    //                     code:200,
    //                     msg:"找不到该会员"
    //                 }
    //             }
    //         } catch (error) {
    //             ctx.body = {
    //                 code:500,
    //                 msg:"启用失败"+error
    //             }
    //         }
    //     }
        
        
    // },
    DELETEVIP:async (ctx, next)=>{
        const {id,status } = ctx.request.body
        // 批量禁用
        if(id.length > 1 && status==0 && Array.isArray(id)){
            try {
               id.forEach(async (item)=>{
                    await db.promise().query('update viplist set status=1 where id=?',item)
                })
                ctx.body = {
                    code:200,
                    msg:"批量禁用成功"
                }
            } catch (error) {
                ctx.body = {
                    code:500,
                    msg:"禁用失败"+error
                }
            }
        }
        // 批量启用
        else if(id.length > 1 && status==1 &&  Array.isArray(id)){
            try {
                id.forEach(async (item)=>{
                     await db.promise().query('update viplist set status=0 where id=?',item)
                 })
                 ctx.body = {
                     code:200,
                     msg:"批量启用成功"
                 }
             } catch (error) {
                 ctx.body = {
                     code:500,
                     msg:"禁用失败"+error
                 }
             }
        }else if(typeof id === 'string'  && status==0){
            
                    // 禁用
                    try {
                        let [res] =await db.promise().query('update viplist set status=1 where id=?',id)
                        if(res.affectedRows !==0){
                            ctx.body = {
                                code:200,
                                msg:"禁用成功"
                            }
                        }else{
                            ctx.body = {
                                code:200,
                                msg:"找不到该会员"
                            }
                        }
                    } catch (error) {
                        ctx.body = {
                            code:500,
                            msg:"禁用失败"+error
                        }
                    }
                }else  if( typeof id === 'string' && status==1){
                    // 启用
                    try {
                        let [res] =await db.promise().query('update viplist set status=0 where id=?',id)
                        if(res.affectedRows !==0){
                            ctx.body = {
                                code:200,
                                msg:"启用成功"
                            }
                        }else{
                            ctx.body = {
                                code:200,
                                msg:"找不到该会员"
                            }
                        }
                    } catch (error) {
                        ctx.body = {
                            code:500,
                            msg:"启用失败"+error
                        }
                    }
                }

    },
    // 修改会员
    UPDATEVIP:async (ctx, next)=>{
        const {vipName,sex,vipPhone,expires,vipRank,money,score,openStaff,status,id} =ctx.request.body
        try {
            let res =await db.promise().query('update viplist set vipName=?,sex=?,vipPhone=?,expires=?,vipRank=?,money=?,score=?,openStaff=?,status=? where id=?',
            [vipName,sex,vipPhone,expires,vipRank,money,score,openStaff,status,id])
            if(res.affectedRows !== 0){
                ctx.body = {
                    code:200,
                    msg:"更新成功"
                }
            }else{
                ctx.body = {
                    code:500,
                    msg:"未找到该会员"
                }
            }
        } catch (error) {
            ctx.body = {
                code:500,
                msg:"更新失败"+error
            }
        }
    },
    // 今日过生
    GETTODAYBORN:async (ctx, next)=>{  
        let newDate = new Date()
         let Today = formatDate(newDate)
         let endMonth1 = endMonth(newDate)
         let nextEndMonth1 = nextEndMonth(newDate)
       try {
        let [res] = await db.promise().query('SELECT * FROM viplist WHERE born = ?',[Today])
        let [res1] = await db.promise().query('SELECT * FROM viplist where born between ? and ?',[Today,endMonth1])
        let [res11] = await db.promise().query('SELECT * FROM viplist where born between ? and ?',[endMonth1,nextEndMonth1])
        ctx.body = {
            code:200,
            msg:'获取成功',
            bornData:{
                todayBorn:res.length,
                monthBorn:res1.length,
                nextMonthBorn :res11.length
            }
         }
       } catch (error) {
        ctx.body = {
            code:201,
            msg: '获取失败'+ error
         }
       }

       
    },
    // 今天过期
    GETTODAYEXPIRES:async (ctx, next)=>{  
        
        let newDate = new Date()
        let Today = formatDate(newDate)
        let endMonth1 = endMonth(newDate)
        let nextEndMonth1 = nextEndMonth(newDate)
        try {
            let [res] = await db.promise().query('SELECT * FROM viplist WHERE expires = ?',[Today])
            let [res1] = await db.promise().query('SELECT * FROM viplist where expires between ? and ?',[Today,endMonth1])
            let [res11] = await db.promise().query('SELECT * FROM viplist where expires between ? and ?',[endMonth1,nextEndMonth1])
            ctx.body = {
                code:200,
                msg:'获取成功',
                expiresData:{
                    todayExpires:res.length,
                    monthExpires:res1.length,
                    nextMonthExpires:res11.length,
                }
             }
           } catch (error) {
            ctx.body = {
                code:201,
                msg: '获取失败'+ error
             }
           }
    },
    // 今日新增会员
    GETTODAYADDVIP:async (ctx, next)=>{  
        let newDate = new Date()
        let Today = formatDate(newDate)
        try {
            let [res] = await db.promise().query('SELECT * FROM viplist WHERE createTime = ?',[Today])
            let [res1] = await db.promise().query('SELECT * FROM viplist WHERE createTime >= ? - INTERVAL 6 DAY',[Today])
            let [res11] = await db.promise().query('SELECT * FROM viplist WHERE createTime >= ? - INTERVAL 30 DAY',[Today])
            ctx.body = {
                code:200,
                msg:'获取成功',
                addVipData:{
                    todayAddVip:res.length,
                    sevenAddVip:res1.length,
                    thirtyAddVip:res11.length,
                }
             }
           } catch (error) {
            ctx.body = {
                code:201,
                msg: '获取失败'+ error
             }
           }
    },
    // 本月未消费
    GETTHIRTYNOTCONSUM:async (ctx, next)=>{  
        let newDate = new Date()
        let Today = formatDate(newDate)
        try {
            let [res] = await db.promise().query('SELECT * FROM viplist WHERE finalConsumTime <= ? - INTERVAL 30 DAY',[Today])
            let [res1] = await db.promise().query('SELECT * FROM viplist WHERE finalConsumTime <= ? - INTERVAL 60 DAY',[Today])
            let [res11] = await db.promise().query('SELECT * FROM viplist WHERE finalConsumTime <= ? - INTERVAL 90 DAY',[Today])
            ctx.body = {
                code:200,
                msg:'获取成功',
                notConsumData:{
                    thirtyDayNotConsum:res.length,
                    sixtyDayNotConsum:res1.length,
                    ninetyDayNotConsum:res11.length
                }
             }
           } catch (error) {
            ctx.body = {
                code:201,
                msg: '获取失败'+ error
             }
           }
    },
    // 获取会员等级
    GETVIPRANK: async (ctx, next) => {
        const {startPage,endPage} = await paging(ctx)
        const {belongToShop} = ctx.request.query
        let [total] = await db.promise().query('select * from rank ')
        try {
            let [res] = await db.promise().query('select *  from rank where belongToShop=? order by createTime desc limit ?,?',
            [belongToShop,startPage,endPage])
            ctx.body = {
                code:200,
                msg:'获取vip等级成功',
                total:total.length,
                vipRankList:res
            }
        } catch (error) {
            ctx.body = {
                code:201,
                msg:'获取vip等级失败'+error
            }
        }
       
    },
    // 添加会员等级
    ADDVIPRANK: async (ctx, next) => {
        const {id,rankName,discount,belongToShop} =ctx.request.body 
        const oldCreateTime = new Date()
        const createTime = formatDate(oldCreateTime)
        try {
            let res =  await db.promise()
            .query('insert into rank(id,rankName,discount,belongToShop,createTime) values(?,?,?,?,?)',
            [id,rankName,discount,belongToShop,createTime])
            if (res[0].affectedRows > 0) {
                ctx.body = {
                    code: 200,
                    msg: '添加会员等级成功'
                }
            }
        } catch (error) {
            console.log(error);
            ctx.body = {
                code: 500,
                msg: '添加会员等级失败'+error
            }
        }
       
    },
    // 更新会员等级
    UPDATEVIPRANK: async (ctx, next) => {
        const {id,rankName,discount,belongToShop} =ctx.request.body 
        try {
            let res =await db.promise().query('update rank set rankName=?,discount=?,belongToShop=? where id=?',
            [rankName,discount,belongToShop,id])
            if(res.affectedRows !== 0){
                ctx.body = {
                    code:200,
                    msg:"更新成功"
                }
            }else{
                ctx.body = {
                    code:500,
                    msg:"未找到该会员"
                }
            }
        } catch (error) {
            ctx.body = {
                code:500,
                msg:"更新失败"+error
            }
        }
       
    },
    // 删除会员等级
    DELVIPRANK: async (ctx, next) => {
        const {id} = ctx.request.body
        try {
            let [res] = await db.promise().query('delete from rank where id=?',id)
            if(res.affectedRows > 0 ){
                ctx.body ={
                    code:200,
                    msg:'删除成功',
                }           
             }
        } catch (error) {
            ctx.body ={
                code:500,
                msg:'删除失败'+error,
            }
        }
       
    },
    // 搜索会员等级
    SEARCHVIPRANK: async (ctx, next) => {
        const {keyWord, belongToShop} = ctx.request.body
        try {
            let [rankNameRes] = await db.promise().query('select * from rank where rankName=? and belongToShop=?' ,[keyWord, belongToShop])
            if(rankNameRes.length !==0){
                ctx.body = {
                    code:200,
                    msg:"搜索成功",
                    ranklist:rankNameRes
                }
            }else{
                ctx.body = {
                    code:201,
                    msg:"没有该会员",
                }
            }
        } catch (error) {
            ctx.body = {
                code:500,
                msg:"搜索失败" + error
            }
        }
       
    },
    // 更新会员积分
    UPDATEVIPSCORE: async (ctx, next) => {
        const {id, score} =ctx.request.body 
        try {
            let res =await db.promise().query('update viplist set score=?where id=?',
            [ score,id])
            if(res.affectedRows !== 0){
                ctx.body = {
                    code:200,
                    msg:"更新成功"
                }
            }else{
                ctx.body = {
                    code:500,
                    msg:"未找到该会员"
                }
            }
        } catch (error) {
            ctx.body = {
                code:500,
                msg:"更新失败"+error
            }
        }
       
    },
}